package com.beikai.elasticsearch_demo.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.RandomUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;

public class AESUtil {
    /**
     * 字符串默认键值
     */
    private String strKey;

    /**
     * 加密工具
     */
    private Cipher encryptCipher = null;

    /**
     * 解密工具
     */
    private Cipher decryptCipher = null;

    private static byte[] AESKeys = {0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D,
            0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F};

    public AESUtil(String strKey) throws Exception {
        // 为了能与 iOS 统一, 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
        this.strKey = String.format("%-16s", strKey).substring(0, 16);
        Key key = new SecretKeySpec(this.strKey.getBytes("utf-8"), "AES");

        encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        //encryptCipher = Cipher.getInstance("DES/ECB/NoPadding");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(AESKeys));
        encryptCipher.getIV();

        decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        //decryptCipher = Cipher.getInstance("AES/CBC/NoPadding");
        decryptCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(AESKeys));
    }

    public String getStrKey() {
        return strKey;
    }

    public String encode(String encryptString) throws IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] bytes = this.encryptCipher.doFinal(encryptString.getBytes("UTF-8"));
        return Base64.encodeBase64String(bytes);
    }

    public String decode(String decryptBase64String) {
        try {
            byte[] base64Bytes = Base64.decodeBase64(decryptBase64String.getBytes("UTF-8"));
            byte[] bytes = this.decryptCipher.doFinal(base64Bytes);
            return new String(bytes, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {


        try {
            for(int i=0;i<1000000;i++ ){
                int kk= RandomUtils.nextInt(10,20);
                System.out.println(kk);
                if(kk==20){
                    System.out.println(kk);
                }
        }
            //String a = new String(AESKeys, "utf-8");
            //System.out.println(a);
            //System.out.println(AESKeys.length);
            //System.out.println(HexUtil.bytes2HexString(AESKeys));

            //String keyString = String.format("%-16s", "adfasdfasd").substring(0, 16);
            //System.out.println(keyString);
            //System.out.println(HexUtil.bytes2HexString(keyString.getBytes("utf-8")));

//			AESUtil aesUtil = new AESUtil("AB58ED1C29FD3AB7304BBB7D5D0D435E");
//            AESUtil aesUtil = new AESUtil("DYKJ-YGOL");
//			String tmp = aesUtil.encode("Ed7r15SKy+cqZfXRaFcCNA==");
//			System.out.println(tmp);
            //System.out.println(ZipUtil.gzip(tmp));
            //System.out.println("<Package><Header><RequestType>03</RequestType><SendTime>2018-09-26 15:26:34</SendTime><ThirdSerial>LJS153786039439877771234</ThirdSerial><Asyn>0</Asyn><ProductCode>TAXPLANCODE</ProductCode></Header><Request><Proposal><ProposalNo>1002950000012218</ProposalNo></Proposal><Payment><OrderId>LJS153786039439877774229</OrderId><PayOrderId>LJS153786039439877774229</PayOrderId><PayAccountId>6222020406007287901</PayAccountId><PayTime>2018-09-26 14:50:34</PayTime><PayMoney>31000</PayMoney><AccountDate>2018-09-26 14:50:34</AccountDate><PayType>UNIONPAY</PayType><BankCode>CCB</BankCode><BankCardNo>6222020406007287901</BankCardNo><HolderName>张雨晴</HolderName><BankAccount>工商银行</BankAccount></Payment></Request></Package>");
//            System.out.println(aesUtil.decode("Ed7r15SKy+cqZfXRaFcCNA=="));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
